#ifndef URL_REQUEST_CLIENT_H
#define URL_REQUEST_CLIENT_H
#include <string>
#include "include/cef_urlrequest.h"
#include "include/wrapper/cef_helpers.h"

class UrlRequestCompletionCallback
{
public:
	virtual ~UrlRequestCompletionCallback() {}
	virtual void OnCompletion(CefURLRequest::ErrorCode errorCode,
		const std::string& data) = 0;
	virtual void OnRequestFailed() = 0;
};


class UrlRequestClient : public CefURLRequestClient
{
public:
	UrlRequestClient()
		: m_callback(0)
	{
		CEF_REQUIRE_UI_THREAD();
	}

	UrlRequestClient(UrlRequestCompletionCallback *callback)
		: m_callback(callback)
	{
		CEF_REQUIRE_UI_THREAD();
	}

	~UrlRequestClient();
	//
	//interfaces of CefURLRequestClient
	//
	void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE;

	void OnUploadProgress(CefRefPtr<CefURLRequest> request,
		int64 current,
		int64 total) OVERRIDE;

	void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
		int64 current,
		int64 total) OVERRIDE;

	void OnDownloadData(CefRefPtr<CefURLRequest> request,
		const void* data,
		size_t data_length) OVERRIDE;

	bool GetAuthCredentials(bool isProxy,
		const CefString& host,
		int port,
		const CefString& realm,
		const CefString& scheme,
		CefRefPtr<CefAuthCallback> callback) OVERRIDE {
		return false;
	}

	void Request(CefRefPtr<CefRequest> cef_request);
	void Get(const std::string &url, const CefRequest::HeaderMap &headers = CefRequest::HeaderMap());
	void Post(const std::string &url, const CefRefPtr<CefPostData> data, const CefRequest::HeaderMap &headers = CefRequest::HeaderMap());

	void SetCompletionCallback(UrlRequestCompletionCallback *callback)
	{
		m_callback = callback;
	}

private:
	UrlRequestCompletionCallback *m_callback;
	CefRefPtr<CefURLRequest> m_urlRequest;
	std::string m_data;

	IMPLEMENT_REFCOUNTING(UrlRequestClient);
	DISALLOW_COPY_AND_ASSIGN(UrlRequestClient);
};

class PrintUrlReqCallback : public UrlRequestCompletionCallback
{
public:
	void OnCompletion(CefURLRequest::ErrorCode errorCode, const std::string& data);
};

#endif